package com.sgb.zhaocai.view.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.link.entity.PickInvitedApply;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.databinding.ActivityPickInvitedWebsiteBinding;
import com.sgb.zhaocai.entity.AddInviteExternalListBean;
import com.sgb.zhaocai.entity.ProjectListEntity;
import com.sgb.zhaocai.view.adapter.WebSiteAdapter;
import com.sgb.zhaocai.view.demand.adapter.ProjectAdapter;
import com.sgb.zhaocai.viewmodel.PickInvitedTheWebSiteModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/12/20
 * AUTHOR:F_Q
 * Description:邀请报名和邀请竞价-邀请非网站界面
 */
public class PickInvitedTheWebSiteActivity extends AppActivity {

    private ActivityPickInvitedWebsiteBinding mBinding;
    private PickInvitedTheWebSiteModel mModel;
    private int ADDINVITEDWEBSITE = 100201;
    private PickInvitedApply bean;
    AddInviteExternalListBean DataBean = new AddInviteExternalListBean();
    List<AddInviteExternalListBean.AddInviteDataBean> lsibean = new ArrayList<>();
    private int EDITORDATA = 100202;
    private int editorPos;
    private WebSiteAdapter websiteadapter;
    private String fromWhere = "";
    private String type = "";
    private String time = "";

    public static void start(Context activity, PickInvitedApply pickinvitedapply, String fromWhere) {
        Intent intent = new Intent();
        intent.setClass(activity, PickInvitedTheWebSiteActivity.class);
        intent.putExtra("Data", (Serializable) pickinvitedapply);
        intent.putExtra("fromWhere", fromWhere);
        activity.startActivity(intent);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_pick_invited_website);
        mModel = new ViewModelProvider(this).get(PickInvitedTheWebSiteModel.class);
        mModel.setActivity(PickInvitedTheWebSiteActivity.this);
        mBinding.includeTitle.ivBack.setOnClickListener(v -> finish());
        mBinding.includeTitle.titleView.setText("邀请非网站客户");
        mBinding.includeTitle.rightView.setText("新增");
        bean = (PickInvitedApply) getIntent().getSerializableExtra("Data");
        fromWhere = getIntent().getStringExtra("fromWhere");
        mBinding.includeTitle.rightView.setOnClickListener(v -> startData());
        mBinding.add.setOnClickListener(v -> startData());
        //提交数据
        mBinding.invitedSure.setOnClickListener(v -> invitedSureData());
    }


    private void startData() {
        Intent intent = new Intent(this, PickAddInvitedWebSiteActivity.class);
        this.startActivityForResult(intent, ADDINVITEDWEBSITE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == ADDINVITEDWEBSITE) {
                AddInviteExternalListBean.AddInviteDataBean dataBean = (AddInviteExternalListBean.AddInviteDataBean) data.getSerializableExtra("data");
                lsibean.add(dataBean);
                if (lsibean != null && lsibean.size() > 0) {
                    mBinding.llList.setVisibility(View.VISIBLE);
                    mBinding.empty.setVisibility(View.GONE);
                    listViewData();
                } else {
                    mBinding.llList.setVisibility(View.GONE);
                    mBinding.empty.setVisibility(View.VISIBLE);
                }
                //更新确认邀请按钮
                invitedSureDataView();
            }
            if (requestCode == EDITORDATA) {
                AddInviteExternalListBean.AddInviteDataBean dataBean = (AddInviteExternalListBean.AddInviteDataBean) data.getSerializableExtra("data");
                AddInviteExternalListBean.AddInviteDataBean inviteDataBean = lsibean.get(editorPos);
                inviteDataBean.setTelephone(dataBean.getTelephone());
                inviteDataBean.setCompanyName(dataBean.getCompanyName());
                inviteDataBean.setContacts(dataBean.getContacts());
                websiteadapter.notifyDataSetChanged();
            }
            if (requestCode == 100203) {

            }
        }
    }


    private void listViewData() {
        websiteadapter = new WebSiteAdapter(getActivity(), lsibean);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mBinding.recyclerview.setLayoutManager(layoutManager);
        mBinding.recyclerview.setAdapter(websiteadapter);
        websiteadapter.onIChildClick(new WebSiteAdapter.IChildClick() {
            @Override
            public void editor(int position) {
                editorPos = position;
                //编辑
                starteditorData(position);
            }

            @Override
            public void delete(int position) {
                //删除
                if (lsibean != null && lsibean.size() > 0) {
                    lsibean.remove(position);
                    websiteadapter.notifyDataSetChanged();
                    if (lsibean.size()>0){
                        mBinding.llList.setVisibility(View.VISIBLE);
                        mBinding.empty.setVisibility(View.GONE);
                    }else {
                        mBinding.llList.setVisibility(View.GONE);
                        mBinding.empty.setVisibility(View.VISIBLE);
                    }
                } else {
                    mBinding.llList.setVisibility(View.GONE);
                    mBinding.empty.setVisibility(View.VISIBLE);
                }
                //更新确认邀请按钮
                invitedSureDataView();
            }
        });
    }

    private void starteditorData(int pos) {
        Intent intent = new Intent(this, PickAddInvitedWebSiteActivity.class);
        intent.putExtra("data", (Serializable) lsibean.get(pos));
        this.startActivityForResult(intent, EDITORDATA);
    }

    //更新确认邀请按钮
    private void invitedSureDataView() {
        mBinding.invitedSure.setText("确认邀请(" + websiteadapter.getDatas().size() + ")");
    }

    //向下一个界面提交数据
    private void invitedSureData() {
        if (websiteadapter != null && websiteadapter.getDatas().size() > 0) {
            List<String> companyList = new ArrayList<>();
            List<String> companynameList = new ArrayList<>();
            if (TextUtils.equals(fromWhere, "1")) {
                type = "3";
                if (!TextUtils.isEmpty(bean.getApplyEndTime())){
                    time =   bean.getApplyEndTime();
                }else {
                    time = "";
                }
            } else {
                type = "4";
                time = "";
            }
            for (AddInviteExternalListBean.AddInviteDataBean data : websiteadapter.getDatas()) {
                companyList.add(data.getCompanyName());
                companynameList.add(data.getCompanyName());
            }
            DataBean.setAddInviteExternalDTOList(lsibean);
            DataBean.setBusinessId(bean.getId());
            PickInvitedApplyDetailsActivity.start(PickInvitedTheWebSiteActivity.this, type, bean.getName(), time, 0, companyList,bean.getId(),new ArrayList<>(),companynameList,new ArrayList<>(),100203,"",DataBean);
        } else {
            MToast.showToast(this, "请添加客户信息");
        }
    }

}
